

class.tfBtn = extends(class.button) {
    reload = true,
    line_iocn1 = [[ReplaceableTextures\TeamColor\TeamColor15.blp]],
    line_iocn2 = [[ReplaceableTextures\TeamColor\TeamColor09.blp]],
    line_iocn3 = [[ReplaceableTextures\TeamColor\TeamColor05.blp]],
    new = function (parent,x,y,w,h)
        local panel = class.tfBtn:builder 
        {
            parent = parent,
            x = x,
            y = y,
            w = w,
            h = h,
        }
        return panel
    end,

    init = function (self)
        class.button.init(self)
        self:set_normal_image([[textures\black32.blp]])
        self:set_highlight([[UI\Buttons\HeroLevel\HeroLevel-Border.blp]])
        --角标
        self.overlay = self:child_builder
        {
            parent = self,
            type = 'panel',
            normal_image = [[UI\Widgets\Console\Human\CommandButton\human-button-lvls-overlay.blp]],
            x = self.w*0.5,
            y = self.h*0.5,
            w = self.w*0.5,
            h = self.h*0.5,
        }
        self.overlay.text = self.overlay:add_text('',0,self.overlay.h*0.2,self.overlay.w,self.overlay.h*0.8,8,5)
        self.overlay:hide()
        self.line_list = {}
        return self
    end,

    --[设置等级]
    set_overlay = function(self,level)
        self.open = level and level>0
        if self.open then
            self.overlay.text:set_text(level)
            self.overlay:show()
            self:set_active()
        else
            self.overlay:hide()
        end
    end,
    
    --[创建线] 用父级创建防止按钮层级问题
    create_line = function(self,x,y,w,h)
        local parent = self.parent
        local x0,y0 = self:get_position()
        local x1 = x0 + x
        local y1 = y0 + y
        local id = ('%s-%s-%s-%s'):format(x1,y1,w,h)
        local public_line = parent.public_line or {}
        parent.public_line = public_line
        if public_line[id] then
            return public_line[id]
        end
        local line = parent:add_panel(self.line_iocn1,x1,y1,w,h)
        line.highlight1 = line:add_panel('')
        public_line[id] = line
        return line
    end,

    --[显示线路] 黄线
    show_line = function(self,name)
        local list = self.line_list[name]
        if self.open then
            for _,line in ipairs(list or {}) do
                line.highlight1:set_normal_image(self.line_iocn3)
            end
        end
    end,
    
    --[激活白线]
    set_active = function(self)
        for _,list in pairs(self.line_list) do
            for _,line in ipairs(list or {}) do
                line.highlight1:set_normal_image(self.line_iocn2)
            end
        end
    end,
    
    --[重置显示]
    reset_show = function(self)
        for _,list in pairs(self.line_list) do
            for _,line in ipairs(list or {}) do
                line.highlight1:set_normal_image('')
            end
        end
    end,

    --[添加一个线路]从左倒右画
    add_line = function(self,name,ui)
        local gap = 5
        local x1,y1 = self:get_real_center()
        local x2,y2 = ui:get_real_center()
        local x3 = self.w
        local y3 = (self.h - gap)/2
        local w1 = (x2 - x1)
        local y4 = (y2 - y1) + (ui.h - gap)/2
        local list = {}
        self.line_list[name] = list
        if y3==y4 then
            local line = self:create_line(x3,y3,w1 - (self.w + ui.w)/2,gap)
            table.insert(list,line)
        else
            local w2 = math.min((w1-(self.w + ui.w)/2)*0.5,20)
            local h1 = math.abs(y3 - y4)
            local x5 = x3 + w2 - gap
            local y5 = y3 - h1
            --一条公用线
            if not self.public_line then
                self.public_line = self:create_line(x3,y3,w2,gap)
            end
            local y6 = y5 + gap
            local y7 = y5
            if y3<y4 then
                y6 = y6 + h1
                y7 = y6 + h1 - gap
            end
            local line1 = self:create_line(x5,y6,gap,h1 - gap)
            local line2 = self:create_line(x5,y7,w1 - x5,gap)
            
            table.insert(list,self.public_line)
            table.insert(list,line1)
            table.insert(list,line2)
        end
    end,
    
}

